package com.example.testgit.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.testgit.VO.ActivityVO;
import com.example.testgit.VO.DeepCoachVO;
import com.example.testgit.VO.PageResult;
import com.example.testgit.VO.StudentQuery;
import com.example.testgit.entity.ActivityDO;
import com.example.testgit.entity.DeepCoachDO;
import com.example.testgit.mapper.ActivityMapper;
import com.example.testgit.mapper.DeepCoachMapper;
import com.example.testgit.mapstruct.ActivityConverter;
import com.example.testgit.mapstruct.DeepCoachConverter;
import com.example.testgit.service.SecondClassroomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SecondClassroomServiceImpl implements SecondClassroomService{

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private DeepCoachMapper deepCoachMapper;

    @Override
    public PageResult<ActivityVO> queryActivity(StudentQuery stu){

        QueryWrapper<ActivityDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",stu.getStuId());

        Page<ActivityDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());
        Page<ActivityDO> activityDoPage = activityMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(activityDoPage);
        pageResult.setData(ActivityConverter.INSTANCE.toVOList(activityDoPage.getRecords()));

        // 活动数据总览计算 count:参加活动次数，newCount:本学期次数，credits:活动总积分，newCredits:本学期积分
        int count = 0;
        int newCount = 0;
        int credits = 0;
        int newCredits = 0;

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String locDate = formatter.format(date);
        System.out.println("********"+ locDate +"********");
        List<ActivityVO> activityVOS = ActivityConverter.INSTANCE.toVOList(activityMapper.selectList(wrapper));

        for (ActivityVO activityVO : activityVOS){
            count++;
            credits += activityVO.getActivityCredits();

            String[] year = activityVO.getActivityTime().split("年");
            String[] moonth = year[1].split("月");
            String[] newmoonth = locDate.split("-");
            String[] newYear = newmoonth[2].split(" ");

            if(year[0].equals(newYear[0])){
                if((Integer.parseInt(newmoonth[1]) > 1) && (Integer.parseInt(newmoonth[1]) < 8) && (Integer.parseInt(moonth[0]) < 8) && (Integer.parseInt(moonth[0]) > 1)) {
                    newCount++;
                    newCredits += activityVO.getActivityCredits();
                }
                if((Integer.parseInt(newmoonth[1]) > 8) && (Integer.parseInt(moonth[0]) > 8)) {
                    newCount++;
                    newCredits += activityVO.getActivityCredits();
                }
            }else {
                if(((Integer.parseInt(newYear[0])-Integer.parseInt(year[0])) == 1) && (Integer.parseInt(newmoonth[1]) < 2) && (Integer.parseInt(moonth[0]) > 8)) {
                    newCount++;
                    newCredits += activityVO.getActivityCredits();
                }
            }
        }

        JSONObject json = new JSONObject();
        json.put("count", count);
        json.put("newCount", newCount);
        json.put("credits", credits);
        json.put("newCredits", newCredits);
        pageResult.setNote(json);

        return pageResult;
    }

    @Override
    public PageResult<DeepCoachVO> queryDeepCoach(StudentQuery stu){

        QueryWrapper<DeepCoachDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",stu.getStuId());

        Page<DeepCoachDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());
        Page<DeepCoachDO> deepCoachDoPage = deepCoachMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(deepCoachDoPage);
        pageResult.setData(DeepCoachConverter.INSTANCE.toVOList(deepCoachDoPage.getRecords()));

        // 深度辅导数据总览计算 count:深度辅导次数，newCount:本学期次数
        int count = 0;
        int newCount = 0;

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String locDate = formatter.format(date);
        System.out.println("********"+ locDate +"********");
        List<DeepCoachVO> deepCoachVOS = DeepCoachConverter.INSTANCE.toVOList(deepCoachMapper.selectList(wrapper));
        for (DeepCoachVO deepCoachVO : deepCoachVOS){
            count++;
            String[] split = deepCoachVO.getCoachTime().split("年");
            String[] moonth = split[1].split("月");
            String[] split2 = locDate.split("-");
            String[] split3 = split2[2].split(" ");
            if(split[0].equals(split3[0])){
                if((Integer.parseInt(split2[1]) > 1) && (Integer.parseInt(split2[1]) < 8) && (Integer.parseInt(moonth[0]) < 8) && (Integer.parseInt(moonth[0]) > 1)) newCount++;
                if((Integer.parseInt(split2[1]) > 8) && (Integer.parseInt(moonth[0]) > 8)) newCount++;
            }else {
                if(((Integer.parseInt(split2[0])-Integer.parseInt(split[0])) == 1) && (Integer.parseInt(split2[1]) < 2) && (Integer.parseInt(moonth[0]) > 8)) newCount++;
            }

        }

        // 柱状图数据计算
        int kaoYan = 0;
        int chuGuo = 0;
        int english = 0;
        int jiuYe = 0;
        for (DeepCoachVO deepCoachVO : deepCoachVOS){
            if(deepCoachVO.getCoachDetails().equals("出国留学"))chuGuo++;
            if(deepCoachVO.getCoachDetails().equals("就业咨询"))jiuYe++;
            if(deepCoachVO.getCoachDetails().equals("考研择校"))kaoYan++;
            if(deepCoachVO.getCoachDetails().equals("英语学习"))english++;
        }
        System.out.println(chuGuo + " || " + jiuYe + " || " + kaoYan+ " || " +english);

        int num[] = {chuGuo,jiuYe,kaoYan,english};
        String[] sss = {"出国留学","就业咨询","考研择校","英语学习"};

        JSONObject json = new JSONObject();
        json.put("count", count);
        json.put("newCount", newCount);
        json.put("Y", num);
        json.put("X", sss);
        pageResult.setNote(json);

        return pageResult;
    }
}
